import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import VideoResource from '@/views/resources/VideoResource.vue'
import Login from '../views/auth/Login.vue'
import Register from '../views/auth/Register.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: () => import('../views/Home.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: { requiresAuth: false }
    },
    {
      path: '/register',
      name: 'Register',
      component: Register,
      meta: { requiresAuth: false }
    },
    {
      path: '/dashboard',
      name: 'Dashboard',
      component: () => import('../views/Dashboard.vue'),
      meta: { requiresAuth: true }
    },
    {
      path: '/profile',
      name: 'Profile',
      component: () => import('../views/Profile.vue'),
      meta: { requiresAuth: true, title: '个人信息设置' }
    },
    {
      path: '/design',
      component: () => import('@/views/design/DesignLayout.vue'), // 新增父组件
      redirect: '/design/new', // 默认重定向到新的设计页面
      children: [
        {
          path: 'new',
          name: 'NewDesign',
          component: () => import('@/views/design/NewDesign.vue'),
          meta: { title: '新建教学设计', requiresAuth: false } // 不需要登录
        },
        {
          path: 'history',
          name: 'DesignHistory',
          component: () => import('@/views/design/DesignHistory.vue'),
          meta: { title: '历史设计', requiresAuth: false }
        }
      ]
    },
    {
      path: '/study',
      component: () => import('@/views/study/AnalysisLayout.vue'), // 新增父组件
      redirect: '/study/analysis', // 默认重定向到新的设计页面
      children: [
        {
          path: 'analysis',
          name: 'Analysis',
          component: () => import('@/views/study/Analysis.vue'),
          meta: { title: '学情分析', requiresAuth: true } // 需要登录
        },
        {
          path: 'per-learn-case/:studentId',
          name: 'PerLearnCase',
          component: () => import('@/views/study/PerLearnCase.vue'),
          meta: { title: '单个学生学情', requiresAuth: true } // 需要登录
        },
        {
          path: 'mul-learn-case',
          name: 'MulLearnCase',
          component: () => import('@/views/study/MulLearnCase.vue'),
          meta: { title: '批量学情分析', requiresAuth: true },
        }
      ]
    },
    {
      path: '/resources',
      component: () => import('@/views/resources/ResourceLayout.vue'),
      children: [
        {
          path: 'video',
          name: 'VideoResource',
          component: () => import('@/views/resources/VideoResource.vue'),
          meta: {
            title: '视频资源',
            requiresAuth: false
          }
        },
        {
          path: 'audio',
          name: 'AudioResource',
          component: () => import('@/views/resources/AudioResource.vue'),
          meta: {
            title: '音频资源',
            requiresAuth: false
          }
        },
        {
          path: 'image',
          name: 'ImageResource',
          component: () => import('@/views/resources/ImageResource.vue'),
          meta: {
            title: '图片资源',
            requiresAuth: false
          }
        },
        {
          path: 'multimedia/history',
          name: 'MultimediaHistory',
          component: () => import('@/views/resources/MultimediaHistory.vue'),
          meta: {
            title: '多媒体资源历史记录',
            requiresAuth: false
          }
        },
        {
          path: '',
          redirect: '/resources/video'
        }
      ]
    },
    {
      path: '/homework',
      component: () => import('@/views/homework/HomeworkLayout.vue'),
      redirect: '/homework/generator',
      children: [
        {
          path: 'generator',
          name: 'HomeworkGenerator',
          component: () => import('@/views/homework/HomeworkGenerator.vue'),
          meta: { title: '习题生成', requiresAuth: false }
        },
        {
          path: 'history',
          name: 'HomeworkHistory',
          component: () => import('@/views/homework/HomeworkHistory.vue'),
          meta: { title: '历史记录', requiresAuth: false }
        }
      ]
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 检查页面是否需要认证
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
  // 检查多种可能的token存储位置
  const isAuthenticated = localStorage.getItem('token') || 
                         localStorage.getItem('accessToken') || 
                         localStorage.getItem('authToken') || 
                         localStorage.getItem('jwtToken')

  if (requiresAuth && !isAuthenticated) {
    // 需要认证但未登录，重定向到登录页
    next('/login')
  } else if (to.path === '/login' && isAuthenticated) {
    // 已登录状态下访问登录页，重定向到首页
    next('/')
  } else {
    // 其他情况正常跳转
    next()
  }
})

export default router